package com.opennews.openplatform

import com.opennews.openplatform.constants.IncomeExpenseTypeConstant
import com.opennews.openplatform.constants.PaymentTypeConstant
import groovy.transform.CompileStatic

@CompileStatic
class IncomeExpenseAnalysisController extends BaseController {
    IncomeExpenseAnalysisService incomeExpenseAnalysisService

    def index() {
        render("IncomeExpenseAnalysisController")
    }

    def queryExistingDistinctYears() {
        def accountGroupId = getClaims().accountGroupId
        def result = incomeExpenseAnalysisService.queryExistingDistinctYears(accountGroupId)

        sendDataResponse(result)
    }

    def querySummaryByExistingYears() {
        def accountGroupId = getClaims().accountGroupId
        def years = incomeExpenseAnalysisService.queryExistingDistinctYears(accountGroupId)
        def result = incomeExpenseAnalysisService.queryAnnualSummaryByYears(accountGroupId, years)

        sendDataResponse(result)
    }

    def queryOverallSummary() {
        def accountGroupId = getClaims().accountGroupId
        def result = incomeExpenseAnalysisService.queryOverallSummary(accountGroupId)

        sendDataResponse(result)
    }

    def queryAnnualMonthlySummary() {
        def year = params.int("year")
        def accountGroupId = getClaims().accountGroupId
        def cashIncomeSummary = incomeExpenseAnalysisService.queryAnnualMonthlySummary(accountGroupId, IncomeExpenseTypeConstant.INCOME, PaymentTypeConstant.CASH, year)
        def cashExpenseSummary = incomeExpenseAnalysisService.queryAnnualMonthlySummary(accountGroupId, IncomeExpenseTypeConstant.EXPENSE, PaymentTypeConstant.CASH, year)
        def creditCardIncomeSummary = incomeExpenseAnalysisService.queryAnnualMonthlySummary(accountGroupId, IncomeExpenseTypeConstant.INCOME, PaymentTypeConstant.CREDIT_CARD, year)
        def creditCardExpenseSummary = incomeExpenseAnalysisService.queryAnnualMonthlySummary(accountGroupId, IncomeExpenseTypeConstant.EXPENSE, PaymentTypeConstant.CREDIT_CARD, year)

        sendDataResponse([
            cashIncomeSummary: cashIncomeSummary,
            cashExpenseSummary: cashExpenseSummary,
            creditCardIncomeSummary: creditCardIncomeSummary,
            creditCardExpenseSummary: creditCardExpenseSummary
        ])
    }

    def queryMonthlyDailySummary() {
        def year = params.int("year")
        def month = params.int("month")
        def accountGroupId = getClaims().accountGroupId
        def cashIncomeSummary = incomeExpenseAnalysisService.queryMonthlyDailySummary(accountGroupId, IncomeExpenseTypeConstant.INCOME, PaymentTypeConstant.CASH, year, month)
        def cashExpenseSummary = incomeExpenseAnalysisService.queryMonthlyDailySummary(accountGroupId, IncomeExpenseTypeConstant.EXPENSE, PaymentTypeConstant.CASH, year, month)
        def creditCardIncomeSummary = incomeExpenseAnalysisService.queryMonthlyDailySummary(accountGroupId, IncomeExpenseTypeConstant.INCOME, PaymentTypeConstant.CREDIT_CARD, year, month)
        def creditCardExpenseSummary = incomeExpenseAnalysisService.queryMonthlyDailySummary(accountGroupId, IncomeExpenseTypeConstant.EXPENSE, PaymentTypeConstant.CREDIT_CARD, year, month)

        sendDataResponse([
            cashIncomeSummary: cashIncomeSummary,
            cashExpenseSummary: cashExpenseSummary,
            creditCardIncomeSummary: creditCardIncomeSummary,
            creditCardExpenseSummary: creditCardExpenseSummary
        ])
    }

    def queryTopRankByAccountGroup() {
        def pageSize = params.int("pageSize")
        def pageIndex = params.int("pageIndex")
        def type = params.type.toString()
        def paymentType = params.paymentType.toString()
        def year = params.int("year")
        def month = params.int("month")
        def accountGroupId = getClaims().accountGroupId
        def result = incomeExpenseAnalysisService.queryTopRankByAccountGroup(accountGroupId, pageSize, pageIndex, type, paymentType, year, month)

        sendDataResponse(result)
    }

    def querySummaryByDetailType() {
        def type = params.type.toString()
        def paymentType = params.paymentType.toString()
        def year = params.int("year")
        def month = params.int("month")
        def accountGroupId = getClaims().accountGroupId

        def result = type == IncomeExpenseTypeConstant.EXPENSE ? incomeExpenseAnalysisService.queryExpenseByDetailType(accountGroupId, paymentType, year, month)
            : incomeExpenseAnalysisService.queryIncomeByDetailType(accountGroupId, paymentType, year, month)

        sendDataResponse(result)
    }
}